import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import Category from '@/views/Category.vue'
import Shopcart from '@/views/Shopcart.vue'
import My from '@/views/My.vue'
import GoodsList from "@/views/GoodsList"
import Goods from "@/views/Goods"
import Themes from "@/views/Themes"
import CategoryList from "@/views/CategoryList"
import Login from "@/views/Login"
import Buy from "@/views/Buy"
import Address from "@/views/Address"
import AddressAdd from "@/views/AddressAdd"
import AddressEdit from "@/views/AddressEdit"
import Quanbu from "@/views/order/Quanbu"
import DinDanxx from "@/views/order/DinDanxx"
import MyDingdna from "@/components/MyDingdna"
import { Toast } from 'vant'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    meta: { title: '首页' }
  },
  {
    path: '/home',
    name: 'home',
    meta: { title: '首页' },
    component: HomeView
  },
  {
    path: '/category',
    name: 'category',
    meta: { title: '分类' },
    component: Category
  },
  {
    path: '/shopcart',
    name: 'shopcart',
    meta: { title: '购物车' },
    component: Shopcart
  },
  {
    path: '/my',
    name: 'my',
    meta: { title: '我的' },
    component: My
  },
  {
    path: '/goodslist',
    name: 'goodslist',
    meta: { title: '商品列表' },
    component: GoodsList,
    // 路由所有接收到的参数以父组件props方式传给GoodsList
    props: true
  },
  {
    path: '/goodsInfp/:id',
    name: 'goods',
    meta: { title: '商品详情' },
    component: Goods,
    // 路由所有接收到的参数以父组件props方式传给GoodsList
    props: true
  },
  {
    path: '/home/themes',
    name: 'themes',
    meta: { title: '专题详情' },
    component: Themes,
    // 路由所有接收到的参数以父组件props方式传给GoodsList
    props: true
  },
  {
    path: '/category/categorylist',
    name: 'categorylist',
    meta: { title: '分类商品' },
    component: CategoryList,
    // 路由所有接收到的参数以父组件props方式传给GoodsList
    props: true
  },
  {
    path: '/user/login',
    name: 'dl',
    meta: {
      title: '登录'
    },
    component: Login
  },
  {
    path: '/buy',
    name: 'buy',
    meta: {
      title: '订单确认'
    },
    props: true,
    component: Buy
  },
  {
    path: '/user/address',
    name: 'address',
    meta: {
      title: '地址信息'
    },
    component: Address
  },
  {
    path: '/user/addressAdd',
    name: 'addressAdd',
    meta: {
      title: '地址添加'
    },
    component: AddressAdd
  },
  {
    path: '/user/addressedit',
    name: 'addressedit',
    meta: {
      title: '地址修改'
    },
    props: true,
    component: AddressEdit
  },
  {
    path: '/user/address',
    name: 'address',
    meta: {
      title: '地址信息'
    },
    component: Address
  },
  {
    path: '/wodedd',
    name: 'wodedd',
    meta: {
      title: '我的订单'
    },
    component: MyDingdna,
    props: true,
    children: [
      {
        path: '/quanbu',
        name: 'quanbu',
        meta: {
          title: '全部订单'
        },
        props: true,
        component: Quanbu
      },
      {
        path: '/ddxx',
        name: 'ddxx',
        meta: {
          title: '我的订单'
        },
        props: true,
        component: DinDanxx
      }
    ],
    scrollBehavior() {
      // 这个函数将在切换路由时调用
      return { x: 0, y: 0 };
    }
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let needTokenPage = ['buy', 'shopcart', 'order', 'my', 'adress']
  //如果不是去needTokenPage里的路由，直接就进去
  if (!needTokenPage.includes(to.name)) return next();

  // 获取Authorization
  const token = localStorage.getItem("Authorization")
  // Authorization没有跳转去登录
  if (!token) {
    Toast.fail('请先登录')
    router.push("/user/login").catch(r=>{});
  } else {
    next();
  }
})

export default router
